﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Configuration;
using System.Xml.Linq;
using System.IO;

namespace WbsEditor
{
    public sealed class WbsSettingsProvider
        : SettingsProvider
    {
        public override string ApplicationName
        {
            get
            {
                return "WbsEditor";
            }
            set
            {

            }
        }
        private string SettingsPath
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), this.ApplicationName, "settings.xml");
            }
        }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(this.ApplicationName, config);
        }
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            SettingsPropertyValueCollection result = new SettingsPropertyValueCollection();

            XDocument document = this.CreateDocument();
            foreach (SettingsProperty property in collection)
            {
                SettingsPropertyValue value = new SettingsPropertyValue(property);

                XElement element = document.Root.Element(value.Name);
                if (element == null)
                    value.SerializedValue = property.DefaultValue;
                else
                    value.SerializedValue = element.Value;
                value.IsDirty = false;

                result.Add(value);
            }

            return result;
        }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            XDocument document = this.CreateDocument();
            foreach (SettingsPropertyValue value in collection)
            {
                if (!value.IsDirty)
                    continue;

                XElement element = document.Root.Element(value.Name);

                if (element == null)
                {
                    element = new XElement(value.Name);
                    document.Root.Add(element);
                }

                if (value.SerializedValue != null)
                    element.Value = value.SerializedValue.ToString();
            }

            if (!Directory.Exists(Path.GetDirectoryName(this.SettingsPath)))
                Directory.CreateDirectory(Path.GetDirectoryName(this.SettingsPath));

            document.Save(this.SettingsPath);
        }

        private XDocument CreateDocument()
        {
            if (File.Exists(this.SettingsPath))
                return XDocument.Load(this.SettingsPath);

            XDocument result = new XDocument();
            result.Add(new XElement("Settings"));
            return result;
        }
    }
}